package com.demo.camel09.sql;

import com.mysql.cj.jdbc.MysqlDataSource;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry;
import org.apache.log4j.PropertyConfigurator;

public class SqlDemo {

    public static void main(String[] args) throws Exception {

        // 日志
        PropertyConfigurator.configure("src/conf/log4j.properties");
        PropertyConfigurator.configureAndWatch("src/conf/log4j.properties", 1000);

        MysqlDataSource mysqlDataSource = new MysqlDataSource();
        mysqlDataSource.setUrl("jdbc:mysql://192.168.99.100:3306/camel_tutorial");
        mysqlDataSource.setUser("root");
        mysqlDataSource.setPassword("password");

        SimpleRegistry simpleRegistry = new SimpleRegistry();
        simpleRegistry.put("myDataSource", mysqlDataSource);

        CamelContext context = new DefaultCamelContext(simpleRegistry);
        context.start();


        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:start").to("jdbc:myDataSource").bean(new ResultHandler(),"printResult");
            }
        });


        ProducerTemplate producerTemplate = context.createProducerTemplate();
        producerTemplate.sendBody("direct:start", "select * from users");

        // 通用没有具体业务意义的代码，只是为了保证主线程不退出
        synchronized (SqlDemo.class) {
            SqlDemo.class.wait();
        }
    }
}
